package com.wanmait.gohome.controller.manage;

import com.github.pagehelper.PageInfo;
import com.wanmait.gohome.pojo.News;
import com.wanmait.gohome.service.NewsService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Timestamp;
import java.util.UUID;

@Controller
@RequestMapping("/manage/news")
public class NewsController {
    @Resource
    private NewsService newsService;
    @RequestMapping("/newsList")
    public void List(){}
    @PostMapping("/ajaxJsp/newsShow")
    @ResponseBody
    public ModelAndView show(){
        PageInfo<News> newsPageInfo = newsService.selectAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("newsPageInfo",newsPageInfo);
        System.out.println(newsPageInfo);
        mv.setViewName("manage/ajaxJsp/newsShow");
        return mv;
    }
   @GetMapping("selectById")
    @ResponseBody
    public ModelAndView selectById(Integer id){
    News news = newsService.findById(id);
    ModelAndView mv = new ModelAndView();
    mv.addObject("news",news);
    mv.setViewName("manage/news/newsInfo");
    return mv;
    }
    @RequestMapping("update")
    public ModelAndView updateById(Integer id){
        News news = newsService.findById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("news",news);
        return mv;
    }

    @RequestMapping("newsInsert")
    public void insert(){}
   //添加文章
    @PostMapping("insert")
    public ModelAndView insert2(News news, String bb, MultipartFile pic1, HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        String fileName="";
        try {
            String ext = FilenameUtils.getExtension(pic1.getOriginalFilename());//获取扩展名;
            fileName = UUID.randomUUID()+"."+ext;//这个是随机数加上扩展名;
            String realPath = request.getServletContext().getRealPath("/web/userinfo/img/uploadImages"+"/"+fileName);//获得图片路径加上图片名字;
            File file=new File(realPath);//保存图片的路径;
            pic1.transferTo(file);//上传图片到指定路径;
        } catch (Exception e) {
            e.printStackTrace();
        }
       news.setInputTime(Timestamp.valueOf(bb));
        news.setPic(fileName);
        newsService.add(news);
        mv.setViewName("/manage/news/newsList");
        return mv;
    }
    //后台删除文章
    @PostMapping("delete")
    @ResponseBody
    public void deleteNews(Integer id){
        newsService.deleteNews(id);
    }
@PostMapping("updateNews")
    public ModelAndView updateNews(News news,MultipartFile pic1,HttpServletRequest request){
    String fileName="";
    try {
        String ext = FilenameUtils.getExtension(pic1.getOriginalFilename());//获取扩展名;
        fileName = UUID.randomUUID()+"."+ext;//这个是随机数加上扩展名;
        String realPath = request.getServletContext().getRealPath("/web/userinfo/img/uploadImages"+"/"+fileName);//获得图片路径加上图片名字;
        File file=new File(realPath);//保存图片的路径;
        pic1.transferTo(file);//上传图片到指定路径;
    } catch (Exception e) {
        e.printStackTrace();
    }
    news.setPic(fileName);
        newsService.updateNews(news);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/manage/news/newsList");
        return mv;

}
}
